package com.keon.jianbao.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.enu.CheckType;
import com.keon.jianbao.ui.widget.EditTextLayout;
import com.keon.jianbao.util.DialogHelp;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.StringUtils;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.HashMap;

import butterknife.Bind;
import cn.lightsky.infiniteindicator.InfiniteIndicatorLayout;
import cn.lightsky.infiniteindicator.slideview.BaseSliderView;
import cn.lightsky.infiniteindicator.slideview.DefaultSliderView;

/**
 * Created by keon on 2015/9/2.
 */
public class BespeakAppraisalActivity extends BaseActivity {

    String[] catlogs = new String[0];
    String selectedCatlog;

    int type = CheckType.DOOR.getType();
    int select = 0;
    Expert expert = null;

    @Bind(R.id.check_apply_img_view_pager)
    InfiniteIndicatorLayout iilImg;
    @Bind(R.id.check_apply_type)
    Button bType;
    @Bind(R.id.check_apply_submit)
    Button bSubmit;
    @Bind(R.id.check_apply_name)
    EditTextLayout etlName;
    @Bind(R.id.check_apply_phone)
    EditTextLayout etlPhone;
/*    @Bind(R.id.check_apply_expert)
    Button bExpert;*/

    SimpleHandler appraisalBespeakHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                BespeakAppraisalActivity.showToast(result.getTips());
                if (result.getStatus() > 0) {
                    DialogHelp.getMessageDialog(BespeakAppraisalActivity.this, getString(R.string.check_apply_tip), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
//                            AppManager.getAppManager().finishActivity();\
                            onBackPressed();
                        }
                    }).show();
//                    jump(WebActivity.class, bundle);
                } else {
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    SimpleHandler catlogHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    catlogs = JsonUtils.arrayFromJson(result.getContent());
                    bType.setText(catlogs[0]);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_apply;
    }

    @Override
    protected int getToolBarTitle() {
        if (type == CheckType.DOOR.getType()) {
            return R.string.door;
        } else if (type == CheckType.LIGHT.getType()) {
            return R.string.light;
        }
        return R.string.door;
    }

    @Override
    public void initView() {

        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");

        initViewPager();

        bType.setOnClickListener(this);
        bSubmit.setOnClickListener(this);
//        bExpert.setOnClickListener(this);
    }

    @Override
    public void initData() {
        appraisalBespeakHandler.setContext(this);
        catlogHandler.setContext(this);

        showWaitDialog();
        Api.appraisal_catalog(catlogHandler);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.check_apply_type:
                if (catlogs.length > 0) {
                    DialogHelp.getSingleChoiceDialog(this, catlogs, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            selectedCatlog = catlogs[i];
                            bType.setText(selectedCatlog);
                        }
                    }).show();
                } else {
                    showToast(R.string.common_error_network);
                    onBackPressed();
                }
                break;
            case R.id.check_apply_submit:

                if (!check()) {
                    return;
                }
                showWaitDialog();
                if (type == CheckType.DOOR.getType()) {
                    Api.appraisal_bespeak(etlName.getText(), etlPhone.getText(), bType.getText().toString(), expert, 1, appraisalBespeakHandler);
                } else if (type == CheckType.LIGHT.getType()) {
                    Api.appraisal_bespeak(etlName.getText(), etlPhone.getText(), bType.getText().toString(), expert, 2, appraisalBespeakHandler);
                }

                break;
        /*    case R.id.check_apply_expert:
                break;*/
        }
    }

    private void initViewPager() {

        if (type == CheckType.DOOR.getType()) {
            iilImg.setVisibility(View.GONE);
        } else if (type == CheckType.LIGHT.getType()) {
            HashMap<String, Integer> url_maps = new HashMap<String, Integer>();
            url_maps = new HashMap<String, Integer>();

            for (String name : url_maps.keySet()) {
                DefaultSliderView textSliderView = new DefaultSliderView(this);
                textSliderView
                        .image(url_maps.get(name))
                        .setScaleType(BaseSliderView.ScaleType.Fit)
                        .showImageResForEmpty(R.mipmap.page_icon_empty)
                        .showImageResForError(R.mipmap.page_icon_network);
//                    .setOnSliderClickListener(this);
                textSliderView.getBundle()
                        .putString("extra", name);
                iilImg.addSlider(textSliderView);
            }
            iilImg.setScrollDurationFactor(0.7);
            iilImg.setIndicatorPosition(InfiniteIndicatorLayout.IndicatorPosition.Center_Bottom);
            iilImg.startAutoScroll();

            iilImg.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (type == CheckType.LIGHT.getType()) {
            iilImg.stopAutoScroll();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == CheckType.LIGHT.getType()) {
            iilImg.startAutoScroll();
        }
    }

    private boolean check() {
        if (StringUtils.isEmpty(etlName.getText())) {
            showToast(R.string.check_apply_name_error_null);
            return false;
        }
        if (StringUtils.isEmpty(etlPhone.getText())) {
            showToast(R.string.check_apply_phone_error_null);
            return false;
        }
        if (!StringUtils.isMobile(etlPhone.getText())) {
            showToast(R.string.check_apply_phone_error_wrong);
            return false;
        }

        return true;
    }
}
